package test.service;

import org.json.JSONObject;
import org.zw.android.framework.http.FilterNormal;
import org.zw.android.framework.http.HttpFactory;
import org.zw.android.framework.http.HttpRequest;
import org.zw.android.framework.http.HttpUIHandler;
import org.zw.android.framework.http.HttpWrapper;
import org.zw.android.framework.http.ResponseBean;

public class TotooleApi {
	
	public static final String host = "http://openapi.totoole.cn:8081/" ;
	
	public static String openid = "" ;
	public static String token = "" ;
	
	private static FilterApiError Apierror = new FilterApiError() ;
	
	static {
		HttpFactory.setHttpExecutor(new HttpConnect());
		HttpFactory.addFilter(Apierror);
	}
	
	private static HttpRequest baseRequest(String model){
		
		HttpRequest req = new HttpRequest(host + model);
		req.addHeaderParameter("appkey", "c2fa72acd9611e0baba69da4b49ba917");
		req.addHeaderParameter("openid", openid);
		req.addHeaderParameter("accesstoken", token);
		
		return req ;
	}

	public static void journeySquareList(int pageIndex, int pageSize,HttpUIHandler handler){
		
		HttpRequest req = baseRequest("openapi/journey");
		
		req.addParameter("queryType", "A");
		req.addParameter("classes", "9");
		req.addParameter("orderBy", "default");
		req.addParameter("page", "1");
		req.addParameter("rows", "10");
		
		// 执行请求
		HttpFactory.doGet(req, new HttpWrapper() {
			
			@Override
			public Object wrapper(Object result) {
				return result;
			}

			@Override
			public void onError(Object result) {
				
			}
			
		}, handler);
	}
	
	private static class FilterApiError extends FilterNormal {

		@Override
		public boolean filter(ResponseBean bean) {
			
			try{
				JSONObject jo = new JSONObject(bean.getResponse().toString());
				
				if(jo.getString("type").equals("S")){
					bean.setResponse(jo.getString("content"));
					return true ;
				}
				
			} catch(Exception e){
				sendErrorMessage("JSON 错误");
			}
			
			return false;
		}
		
	}
}
